package psd.model.Prolog.Converters;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;

import psd.model.IConverter;
import psd.model.Prolog.FactGenerationToolbox;
import psd.model.Prolog.PrologConverter;


public class PrologSuperMethodInvocationConverter extends PrologConverter
		implements IConverter<SuperMethodInvocation> {

	public PrologSuperMethodInvocationConverter(FactGenerationToolbox toolbox) {
		super(toolbox);
	}

	@Override
	public boolean convert(SuperMethodInvocation node) {
		String selectedFrom = null;
		if (node.getQualifier() == null) {
			TypeDeclaration ultimateAncestor = getUltimateAncestor(node);
			Type superclassType = ultimateAncestor.getSuperclassType();
			String superClassID = superclassType != null ? idResolver
					.getID(superclassType.resolveBinding()) : idResolver
					.getJavaLangObjectID();
			selectedFrom = idResolver.getID();
			String identArgs[] = { selectedFrom, idResolver.getID(node),
					idResolver.getID(getEnclosingNode(node)), "'super'",
					superClassID, this.getFilePath(node), this.getLineNumber(node) };
			writer.writeFact("identT", identArgs);
			writer.writeFact("slT", new String[] { selectedFrom,
					Integer.toString(node.getStartPosition()),
					Integer.toString("super".length()), this.getFilePath(node), this.getLineNumber(node) });
			checkAndGenComments(node, selectedFrom);
			writeSourceLocationArgumentIdentifier(node, node.getName());
		} else {
			ITypeBinding superclass = node.getQualifier().resolveTypeBinding()
					.getSuperclass();
			String superClassID = idResolver.getID(superclass);
			selectedFrom = idResolver.getID();
			String selectArgs[] = {
					selectedFrom,
					idResolver.getID(node),
					idResolver.getID(getEnclosingNode(node)),
					"'super'",
					typeResolver.getTypeTerm((ITypeBinding) node.getQualifier()
							.resolveBinding()), superClassID, this.getFilePath(node), this.getLineNumber(node) };
			writer.writeFact("selectT", selectArgs);
			writer.writeFact("slT", new String[] { idResolver.getID(node),
					Integer.toString(node.getStartPosition()),
					Integer.toString(node.getLength()), this.getFilePath(node), this.getLineNumber(node) });
			checkAndGenComments(node, selectedFrom);
		}
		String args[] = { selectedFrom, quote(node.getName().getIdentifier()),
				idResolver.getIDs(node.arguments()),
				idResolver.getID(node.resolveMethodBinding()), this.getFilePath(node), this.getLineNumber(node) };
		createBodyFact(node, "callT", args);
		writeSourceLocationArgumentIdentifier(node, node.getName());
		return true;
	}

}
